package com.community.controller;

import com.community.base.BaseController;
import com.community.common.Const;
import com.community.common.ServerResponse;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.Map;

/**
 * 回答控制层
 */
@RestController
@RequestMapping("/answerController/")
@CrossOrigin(origins = Const.REQUESTURL,maxAge = 3600,allowCredentials = "true")
public class AnswerController extends BaseController {

    /**
     *  查询优质回答(首页推荐)
     * @param pageNum
     * @param order
     * @param request
     * @return
     */
    @GetMapping("selectGreatAnswer.do")
    public ServerResponse<PageInfo> selectGreatAnswer(@RequestParam(value = "pageNum",defaultValue = "0") int pageNum,
                                                      @RequestParam(value = "order") String order,
                                                      HttpServletRequest request){
        return iAnswerService.selectGreatAnswer(pageNum,order,request);
    }

    /**
     * 添加回答
     * @param paramMap
     * @param request
     * @return
     */
    @PostMapping("addOrUpdateAswer.do")
    public ServerResponse<String> addOrUpdateAswer(@RequestParam Map paramMap, HttpServletRequest request){
        return iAnswerService.addOrUpdate(paramMap,request);
    }

    /**
     * 不显示/显示回答
     * todo 此删除非彼删除
     * @param id
     * @param request
     * @return
     */
    @PostMapping("updateShowStatusById.do")
    public ServerResponse<String> updateShowStatusById(@RequestParam(value = "id") Integer id,
                                                       @RequestParam(value = "show") Integer show,HttpServletRequest request){
        if(null == id || show == null){
            return ServerResponse.createByErrorMessage(Const.Msg.PARAMTERERROR);
        }
        return iAnswerService.updateShowStatusById(id,show,request);
    }

    /**
     * 查看收费答案
     * @param answerId
     * @param request
     * @return
     */
    @GetMapping("viewCharges.do")
    public ServerResponse<String> viewCharges(@RequestParam(value = "answerId") int answerId,
                                              HttpServletRequest request){
        return iAnswerService.viewCharges(answerId,request);
    }

    /**
     * 测试回调函数
     * @param paramMap
     * @param request
     * @return
     */
    @GetMapping("test.do")
    public ServerResponse<String> testCallBack(@RequestParam Map paramMap, HttpServletRequest request){
        Iterator<Map.Entry<String, String>> entries = paramMap.entrySet().iterator();
        //循环存入键值
        while (entries.hasNext()) {
            Map.Entry<String, String> entry = entries.next();
            System.out.println(entry.getKey());
           System.out.println(entry.getValue());
        }
        return ServerResponse.createBySuccessMessage("test");
    }

}


